local yinjiu = fk.CreateSkill{
  name = "hanqing__yinjiu",
}

Fk:loadTranslationTable{
  ["hanqing__yinjiu"] = "引咎",
  [":hanqing__yinjiu"] = "出牌阶段各限一次，你可以发动仅有赢或仅有未赢效果的〖驱虎〗；本阶段均发动后，若两次拼点的赢者不同，则你摸三张牌。",

  ["#hanqing__yinjiu"] = "引咎：发动一次只有赢或没赢效果的“驱虎”，与一名角色拼点",
  ["hanqing__yinjiu1"] = "若赢则令目标造成伤害",
  ["hanqing__yinjiu2"] = "若没赢则目标对你造成伤害",
  ["#hanqing__yinjiu-choose"] = "引咎：选择另一名角色，令 %dest 对其造成1点伤害",

  ["$hanqing__yinjiu1"] = "兽穷，则无物不攫；人穷，则无计不施。",
  ["$hanqing__yinjiu2"] = "祸水如灾洪，不可阻，不可堵，唯有引。",
}

yinjiu:addEffect("active", {
  anim_type = "offensive",
  prompt = "#hanqing__yinjiu",
  card_num = 0,
  target_num = 1,
  interaction = function (self, player)
    local choices = {}
    for i = 1, 2, 1 do
      if player:getMark("hanqing__yinjiu"..i.."-phase") == 0 then
        table.insert(choices, "hanqing__yinjiu"..i)
      end
    end
    if #choices == 0 then return end
    return UI.ComboBox {choices = choices, all_choices = {"hanqing__yinjiu1", "hanqing__yinjiu2"}}
  end,
  can_use = function(self, player)
    return player:getMark("hanqing__yinjiu1-phase") == 0 or player:getMark("hanqing__yinjiu2-phase") == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:setPlayerMark(player, self.interaction.data.."-phase", 1)
    local pindian = player:pindian({target}, yinjiu.name)
    local winner = pindian.results[target].winner
    if winner and not player.dead then
      room:addTableMarkIfNeed(player, "hanqing__yinjiu_winner-phase", winner.id)
    end
    if pindian.results[target].winner == player then
      if self.interaction.data == "hanqing__yinjiu1" and not player.dead and not target.dead then
        local targets = table.filter(room:getOtherPlayers(target, false), function (p)
          return target:inMyAttackRange(p)
        end)
        if #targets == 0 then return end
        local to = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = targets,
          skill_name = yinjiu.name,
          prompt = "#hanqing__yinjiu-choose::"..target.id,
          cancelable = false,
        })[1]
        room:damage{
          from = target,
          to = to,
          damage = 1,
          skillName = yinjiu.name,
        }
      end
    else
      if self.interaction.data == "hanqing__yinjiu2" and not player.dead then
        room:damage{
          from = target,
          to = player,
          damage = 1,
          skillName = yinjiu.name,
        }
      end
    end
  end,
})

yinjiu:addEffect(fk.AfterSkillEffect, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and not player.dead and data.skill.name == yinjiu.name and
      player:getMark("hanqing__yinjiu1-phase") > 0 and player:getMark("hanqing__yinjiu2-phase") > 0 and
      #player:getTableMark("hanqing__yinjiu_winner-phase") == 2
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(3, yinjiu.name)
  end,
})

return yinjiu
